package com.kuzan.autocloud.common.model.form;

import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;

/**
 * <p>Description: </p>
 *
 * @author kuzank
 */
@Data
@NoArgsConstructor
@Entity
@Table(name = "sys_form_field")
public class FormField extends AbsFieldDefine {

    /**
     * 操作类型: 必填、编辑、只读、隐藏
     */
    @Column(length = 12)
    private String operation = FormFieldOperator.Edit.getName();
    /**
     * 监视字段值变更: 是否 - 默认否
     */
    @Column(columnDefinition = "bit(1) default 0")
    private boolean monitor = false;
    /******************* 枚举 & 关联选择项 ********************/
    /**
     * 前置条件
     */
    private String precon;
    /**
     * 脚本
     */
    @Lob
    @Column(columnDefinition = "TEXT")
    private String script;

    public FormField setRequired() {
        this.setOperation(FormFieldOperator.Require.value());
        return this;
    }

    public boolean isRequired() {
        return FormFieldOperator.isRequired(this.getOperation());
    }

    public boolean isReadOnly() {
        return FormFieldOperator.isReadOnly(this.getOperation());
    }

    public boolean isHidden() {
        return FormFieldOperator.isHidden(this.getOperation());
    }

    /**
     * 表单字段操作
     */
    public enum FormFieldOperator {
        pid("ffo"),

        Require("ffo_require"),
        Edit("ffo_edit"),
        Readonly("ffo_readonly"),
        Hidden("ffo_hidden");

        private String name;

        FormFieldOperator(String name) {
            this.name = name;
        }

        public String value() {
            return name;
        }

        public String getName() {
            return name;
        }

        public static boolean isRequired(String operator) {
            return Require.value().equals(operator);
        }

        public static boolean isEdit(String operator) {
            return Edit.value().equals(operator);
        }

        public static boolean isReadOnly(String operator) {
            return Readonly.value().equals(operator);
        }

        public static boolean isHidden(String operator) {
            return Hidden.value().equals(operator);
        }
    }
}
